//
//  RHMePricePatternVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/24.
//

#import "RHMePricePatternVC.h"
#import "JXCategoryView.h"
#import "JXCategoryListContainerView.h"
#import "RHMePriceCommonPatternVC.h"
#import "RHMePriceMyPatternVC.h"

@interface RHMePricePatternVC ()<JXCategoryListContainerViewDelegate,JXCategoryViewDelegate,JXCategoryTitleViewDataSource,QMUITextFieldDelegate>

@property (nonatomic, strong) NSMutableArray *titles;

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@end

@implementation RHMePricePatternVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"使用模板".rh_localized];
    [self.view setBackgroundColor:CFTabViewBgColor];
  
    self.titles = @[@"公共模板".rh_localized,
                    @"我的模板".rh_localized].mutableCopy;
 

    [self setupUI];
}

- (void)setupUI{

    kWeakSelf;
    
    
    _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    _listContainerView.scrollView.scrollEnabled = NO;
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
    self.categoryView.cellWidth = 60;
    self.categoryView.titleSelectedColor = CMainColorGreen;
    self.categoryView.titleColor = kCOLOR_HEX(@"#8B8D92");
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.titleLabelZoomScale = 1.0;
    self.categoryView.cellSpacing = 30;
//    self.categoryView.contentEdgeInsetLeft = 20;
//    self.categoryView.contentEdgeInsetRight = 20;
    self.categoryView.titleLabelVerticalOffset = -10;
    [self.view addSubview:self.categoryView];
    self.categoryView.listContainer = self.listContainerView;
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
     backgroundView.size = CGSizeMake(60, 3);
     backgroundView.indicatorColor = CMainColorGreen;
     backgroundView.indicatorHeight = 3;
     backgroundView.indicatorWidth = 60;
     backgroundView.verticalMargin = -10;
     backgroundView.indicatorCornerRadius = 1.5;
     _categoryView.indicators = @[backgroundView];
     
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(weakSelf.categoryView.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
    
}


#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    kWeakSelf;
   
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return  self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    kWeakSelf;
    if (index == 0) {
        RHMePriceCommonPatternVC *vc = [[RHMePriceCommonPatternVC alloc] init];
        vc.usePatternBlock = ^(RHPatternModel * _Nonnull pattern) {
            if (weakSelf.usePatternBlock) {
                weakSelf.usePatternBlock(pattern);
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        };
        return vc;
    }else {
        RHMePriceMyPatternVC *vc = [[RHMePriceMyPatternVC alloc] init];
        vc.usePatternBlock = ^(RHPatternModel * _Nonnull pattern) {
            if (weakSelf.usePatternBlock) {
                weakSelf.usePatternBlock(pattern);
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
        };
        return vc;
    }
  
}


@end
